package com.example.news;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.news.adapter.UserInfo;
import com.example.news.db.UserDbHelper;

public class UpdatePwdActivity extends AppCompatActivity {

    private EditText et_new_password;
    private EditText et_confirm_password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_pwd);

        et_new_password=findViewById(R.id.et_new_password);
        et_confirm_password=findViewById(R.id.et_confirm_password);

        //返回
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        findViewById(R.id.update_pwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String new_pwd=et_new_password.getText().toString();
                String confirm_pwd=et_confirm_password.getText().toString();
                if(TextUtils.isEmpty(new_pwd)||TextUtils.isEmpty(confirm_pwd)){
                    Toast.makeText(UpdatePwdActivity.this,"信息不能为空",Toast.LENGTH_SHORT).show();
                }else if(new_pwd.equals(confirm_pwd)){
                    Toast.makeText(UpdatePwdActivity.this,"两次不一致",Toast.LENGTH_SHORT).show();
                }else{
                    UserInfo userInfo=UserInfo.getUserInfo();
                    if(null!=userInfo){
                        int row = UserDbHelper.getInstance(UpdatePwdActivity.this).updatePwd(userInfo.getUsername(), new_pwd);
                        if(row>0){
                            Toast.makeText(UpdatePwdActivity.this,"密码修改成功，请重新登录",Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(UpdatePwdActivity.this,LoginActivity.class));
                            UserInfo.setUserInfo(null);
                            finish();
                        }else{
                            Toast.makeText(UpdatePwdActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });
    }
}